package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.dto.CommonSearchDTO;
import com.alibaba.otter.manager.web.dto.NodeDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.NodeAggService;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.config.node.Node;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * node 节点信息配置
 */
@RequestMapping("/api/v1/node")
@RestController
public class NodeResource {

    private final NodeAggService nodeAggService;

    public NodeResource(NodeAggService nodeAggService) {
        this.nodeAggService = nodeAggService;
    }


    @GetMapping("findOne/{id}")
    public RestResponse<NodeDTO> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(nodeAggService.findById(id));
    }


    @GetMapping("list")
    public RestResponse<List<NodeDTO>> list(CommonSearchDTO searchDTO, Pageable pageable){
        return PaginationUtil.pageHttpResponse(nodeAggService.list(searchDTO, pageable));
    }


    @PostMapping("create")
    public RestResponse<Void> create(@RequestBody @Validated Node node){
        nodeAggService.create(node);
        return RestResponse.ok();
    }


    @PutMapping("modify")
    public RestResponse<Void> modify(@RequestBody @Validated Node node){
        nodeAggService.modify(node);
        return RestResponse.ok();
    }


    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        nodeAggService.delete(id);
        return RestResponse.ok();
    }

}
